﻿using System.Windows.Controls;
using Caliburn.Micro;
using System.Windows;
using System.Windows.Documents;

namespace Agnes.Core
{
    /// <summary>
    /// ScreenBase
    /// </summary>
    public abstract class ScreenBase<TModel> : Screen
    {
        private TModel _model;

        /// <summary>
        /// Model
        /// </summary>
        public TModel Model
        {
            get { return _model; }
            set
            {
                _model = value;
                OnModelChanged(_model, value);
                NotifyOfPropertyChange(() => Model);
            }
        }

        /// <summary>
        /// OnModelChanged
        /// </summary>
        /// <param name="oldValue"></param>
        /// <param name="newValue"></param>
        protected virtual void OnModelChanged(TModel oldValue, TModel newValue)
        {
        }

        /// <summary>
        /// DialogResult
        /// </summary>
        protected bool? DialogResult;

        /// <summary>
        /// GetView
        /// </summary>
        /// <param name="context">context</param>
        /// <returns>View</returns>
        public override object GetView(object context = null)
        {
            var result = base.GetView(context);
            if (result == null)
             {
                result = CreateView();
                ViewModelBinder.Bind(this, (result as DependencyObject), this );
            }

            return result;
        }

        /// <summary>
        /// CreateView
        /// </summary>
        /// <returns>View</returns>
        protected abstract object CreateView();

        /// <summary>
        /// TryClose
        /// </summary>
        /// <param name="dialogResult">dialogResult</param>
        public override void TryClose(bool? dialogResult)
        {
            DialogResult = dialogResult;
            base.TryClose(dialogResult);
        }

    }
}
